package com.imooc.controller;

import com.imooc.pojo.SaveModel;
import com.imooc.service.CompareInfoService;
import com.imooc.service.DumpFileService;
import com.imooc.utils.ExcelUtils;
import com.imooc.utils.FileUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Controller
@RequestMapping("file")
public class Upload {

    @Autowired
    private ExcelUtils excelUtils;
	@Autowired
    private CompareInfoService compareInfoService;
    @Autowired
    private DumpFileService dumpFileService;
    @RequestMapping("upload")
    public void pubggupload(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            List<Object> li = new ArrayList<>();
            li.add("文件格式错误");
            return ;
        }
        excelUtils.excelToShopIdList(response, file.getInputStream());
    }


    @RequestMapping("jump")
    public String jumpToUpload(){
        return "thymeleaf/upload";
    }

    @RequestMapping("goods/upload")
    public String uploadGoodsInfo(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            List<Object> li = new ArrayList<>();
            li.add("文件格式错误");
            return "thymeleaf/upload_goods";
        }
        excelUtils.saveImportGoods(file.getInputStream());
        return "thymeleaf/home";
    }

	@RequestMapping("goods/csv")
    public String uploadGoodsInfoCSV(@RequestParam("file") MultipartFile file) throws Exception {
        String name = file.getOriginalFilename();
        if (name.length() < 5 || !name.substring(name.length() - 4).equals(".csv")) {
            return "thymeleaf/upload_goods";
        }
        excelUtils.saveImportGoodsCSV(file.getInputStream());
        return "thymeleaf/home";
    }

    @RequestMapping("goods/jump")
    public String jumpToUploadGoods(){
        return "thymeleaf/upload_goods";
    }


    @RequestMapping("compare/jump")
    public String jumpToUploadCompare(){
        return "thymeleaf/upload_compare";
    }

    /**
     * 中韩汇率计算
     * @param file
     * @param response
     * @throws Exception
     */
    @RequestMapping("compare/upload")
    public String uploadCompare(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            List<Object> li = new ArrayList<>();
            li.add("文件格式错误");
            return "thymeleaf/upload_compare";
        }
        excelUtils.saveImportCompare(file.getInputStream());
        return "thymeleaf/home";
    }

	/**
     * 中韩汇率计算
     * @param response
     * @throws Exception
     */
    @RequestMapping("compare/export")
    public String exportCompare(HttpServletResponse response) throws Exception {

        excelUtils.exportCompareInfoData(response, null);
        return "thymeleaf/home";
    }

    @RequestMapping(value = "/spider")
    public String jumpDump(HttpServletResponse response){
        return "thymeleaf/spider";
    }

    @RequestMapping(value = "excel/url")
    public void htmlFileEx(HttpServletResponse response, String sortTypeValue, int limitNum, int splitNum) {
        StringBuffer url = new StringBuffer();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(System.getProperty("user.dir") + "\\input.txt")));
            String strLine;
            List<SaveModel> list = new ArrayList<>();
            Map<String, Integer> param = new HashMap<>();
            param.put("limitNum", limitNum);
            param.put("splitNum", splitNum);
            param.put("pageStartNo", 10000);
            param.put("pageNo", limitNum / splitNum);
            while(StringUtils.isNotEmpty(strLine = bufferedReader.readLine())){
                url.append("https://store.coupang.com/vp/vendors/");
                url.append(strLine);
                url.append("/product/lists?outboundShippingPlaceId=&attributeFilters=&brand=&componentId=&keyword=&maxPrice=&minPrice=&pageNum=1&rating=0&scpLanding=true");
                url.append("&sortTypeValue=").append(sortTypeValue);
                System.out.println(url);
                param = dumpFileService.exportDownLoad(response, url.toString(), strLine, splitNum, list, param);
                url.setLength(0);
                if (limitNum < 1){
                    break;
                }
            }
            try {
                if (list.size() > 0) {
                    // excel下载
                    String excelPath = System.getProperty("user.dir") + "\\excel\\";
                    FileUtils.mkdirs(excelPath);
                    excelUtils.writeExcel(excelPath + "结果.xlsx", "工作簿1", list);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}